package com.dede.monochromatism.data

import android.graphics.drawable.Drawable
import com.dede.applib.util.HanziToPinyin

/**
 * @author hsh
 * @date 2017/9/19 15:09.
 * @doc
 */
data class AppInfo(var id: Int, var packageName: String, var name: String, var date: String, var icon: Drawable?) :
        Comparable<AppInfo> {

    override fun compareTo(other: AppInfo): Int {
        if (isChecked && !other.isChecked) return -1

        if (!isChecked && other.isChecked) return 1

        if (pinyin != other.pinyin) return pinyin.compareTo(other.pinyin)

        return packageName.compareTo(other.packageName)
    }

    private val sb by lazy { StringBuffer() }

    var isChecked = false

    val pinyin by lazy {
        HanziToPinyin.getInstance().get(name).forEach {
            sb.append(it.target)
        }
        if (sb.isBlank()) sb.append(name)
        sb.toString().toUpperCase()
    }

    var versionName = ""
    var versionCode = 0

    //应用列表
    constructor(packageName: String, name: String, icon: Drawable) :
            this(0, packageName, name, "", icon)

    //解析数据库数据
    constructor(id: Int, packageName: String, name: String, date: String) :
            this(id, packageName, name, date, null)


    override fun toString(): String {
        return "AppInfo(id=$id, packageName='$packageName', name='$name', date='$date')"
    }

    override fun equals(other: Any?): Boolean {
        if (this === other) return true

        if (other == null || javaClass != other.javaClass) return false

        val that = other as AppInfo
        return packageName == that.packageName//只对比包名是否相同
    }

    override fun hashCode(): Int {
        return packageName.hashCode()
    }
}